﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;

namespace EXAM
{
    public class Global : System.Web.HttpApplication
    {
        string sErr = "";
        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Session.Timeout = 30;
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            if (Session["dsTestLog"] != null)
                SaveResult("dsTestLog");
        }
        private void SaveResult(string _dsetName)
        {
            if (Session["FlagTest"] != null)
            {
                DataSet dset = Session[_dsetName] as DataSet;
                long idUrs = Convert.ToInt64(dset.Tables["dtQuest"].Rows[0]["User_ID"]);
                if (idUrs != -1)
                {
                    int index = (int)Session["QuestIndex"];
                    BUS.Test_LogControl logCtr = new BUS.Test_LogControl();
                    long idTest = Convert.ToInt64(dset.Tables["dtQuest"].Rows[0]["Test_ID"]);

                    string type = Convert.ToString(dset.Tables["dtQuest"].Rows[0]["Type"]);
                    if (type == "TMP" && idUrs != -1)
                        sErr = logCtr.Delete(idTest, idUrs, type);
                    BUS.Test_Log_AnswerControl ansCtr = new BUS.Test_Log_AnswerControl();
                    int No = logCtr.GetMaxNo() + 1;
                    foreach (DataRow row in dset.Tables["dtQuest"].Rows)
                    {
                        DTO.Test_LogInfo logInf = new DTO.Test_LogInfo(row);
                        DTO.UserInfo user = Session["UserGuest"] as DTO.UserInfo;
                        if (user == null)
                            Response.Redirect("Login.aspx?hander=save");
                        DataRow[] rows = dset.Tables["dtAnswer"].Select("Test_Log_No = " + logInf.No + " and Test_Log_Line = " + logInf.Line);
                        for (int i = rows.Length - 1; i >= 0; i--)
                        {
                            DataRow rowA = rows[i];
                            DTO.Test_Log_AnswerInfo aInf = new DTO.Test_Log_AnswerInfo(rowA);
                            aInf.Test_Log_No = No;
                            ansCtr.Add(aInf, ref sErr);
                            dset.Tables["dtAnswer"].Rows.Remove(rowA);
                        }
                        logInf.User_ID = user.ID;
                        logInf.No = No;
                        //logInf.Type = "";
                        logCtr.Add(logInf, ref sErr);

                    }
                }
            }
            if (sErr == "")
            {
                Session["QuestIndex"] = null;
                Session[_dsetName] = null;
                Session["Duration_Time"] = null;
                Session["FlagTest"] = null;
                Session["CountAns"] = null;
                //Response.Redirect("TestTemplate.aspx");
            }

        }
        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}